package com.dzwww.recommend.rule.common.api;

import java.io.Serializable;

/**
 * @author winner
 * @param <T> 结果数据类型
 */
public class ApiResult<T> implements Serializable {
    private static final long serialVersionUID = 7678107767150256586L;

    public static <T> ApiResult<T> succeed() {
        return new ApiResult<T>(true);
    }

    public static <T> ApiResult<T> succeed(T data) {
        return new ApiResult<T>(data);
    }

    public static <T> ApiResult<T> succeed(ApiMessage msg, T data) {
        return succeed(msg.getCode(), msg.getMessage(), data);
    }

    public static <T> ApiResult<T> succeed(String code, String msg, T data) {
        return new ApiResult<T>(true, code, msg, data);
    }


    public static <T> ApiResult<T> fail() {
        return new ApiResult<T>(false);
    }

    public static <T> ApiResult<T> fail(ApiMessage msg) {
        return fail(msg.getCode(), msg.getMessage(), null);
    }

    public static <T> ApiResult<T> fail(ApiMessage msg, T data) {
        return fail(msg.getCode(), msg.getMessage(), data);
    }

    public static <T> ApiResult<T> fail(String code, String msg) {
        return fail(code, msg, null);
    }

    public static <T> ApiResult<T> fail(String code, String msg, T data) {
        return new ApiResult<T>(false, code, msg, data);
    }

    /**
     * 是否成功
     */
    private boolean success = true;
    /**
     * 自定义的状态码
     */
    private String code = "";
    /**
     * 返回的消息内容，一般是失败情况的原因说明
     */
    private String message = "";
    /**
     * 返回的数据对象
     */
    private T data;

    private ApiResult(boolean success) {
        this.success = success;
    }

    private ApiResult(T data) {
        this.success = true;
        this.data = data;
    }

    private ApiResult(boolean success, String code, String message, T data) {
        this.success = success;
        this.message = message;
        this.code = code;
        this.data = data;
    }

    public boolean isSuccess() {
        return success;
    }

    public String getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public T getData() {
        return data;
    }

}
